package com.jn.moduledemo.financing;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jn.moduledemo.common.base.BaseActivity;
import com.jn.moduledemo.common.base.BaseFragment;
import com.jn.moduledemo.common.provider.ILoginProvider;
import com.jn.moduledemo.common.utils.LogUtils;

@Route(path = "/financing/FinancingActivity")
public class FinancingActivity extends BaseActivity implements View.OnClickListener {
    private BaseFragment mNowShowFragment;
    private FragmentManager mFragmentManager;
    private HomeFragment mHomeFragment;
    private OrdersFragment mOrdersFragment;
    private BaseFragment mPersonalFragment;
    private ImageView tabA;
    private ImageView tabB;
    private ImageView tabC;
    ILoginProvider mProvider;
    @Override
    public int getLayoutId() {
        return R.layout.activity_financing;
    }

    @Override
    public void init() {
        tabA = findViewById(R.id.iv_f_a);
        tabB = findViewById(R.id.iv_f_b);
        tabC = findViewById(R.id.iv_f_c);
        tabA.setOnClickListener(this);
        tabB.setOnClickListener(this);
        tabC.setOnClickListener(this);
        mFragmentManager = getSupportFragmentManager();
        mHomeFragment = new HomeFragment();
        mOrdersFragment = new OrdersFragment();
        mPersonalFragment = (BaseFragment) ARouter.getInstance()
                .build( "/personal/PersonalFragment" )
                .navigation();
        setNowFragment(mHomeFragment, tabA);
    }

    @Override
    public void onClick(View v) {
        if (isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_f_a) {
            tabA.setSelected(true);
            tabB.setSelected(false);
            tabC.setSelected(false);
            if (mHomeFragment != null) {
                changeFragment(mHomeFragment);
            }
        } else if (id == R.id.iv_f_b) {
            toTabB();
        } else if (id == R.id.iv_f_c) {
            toTabC();
        }
    }

    private void toTabB() {
        if (mProvider == null) {
            mProvider = ARouter.getInstance().navigation(ILoginProvider.class);
        }
        if (!mProvider.getLoginStatus()) {
            ARouter.getInstance()
                    .build("/login/LoginActivity")
                    .withString("from", "tabB")
                    .navigation(this, 1);
        } else {
            tabA.setSelected(false);
            tabB.setSelected(true);
            tabC.setSelected(false);
            if (mOrdersFragment != null) {
                changeFragment(mOrdersFragment);
            }
        }
    }

    private void toTabC() {
        if (mProvider == null) {
            mProvider = ARouter.getInstance().navigation(ILoginProvider.class);
        }
        if (!mProvider.getLoginStatus()) {
            ARouter.getInstance()
                    .build("/login/LoginActivity")
                    .withString("from", "tabC")
                    .navigation(this, 1);
        } else {
            tabA.setSelected(false);
            tabB.setSelected(false);
            tabC.setSelected(true);
            changeFragment(mPersonalFragment);
        }
    }

    private void setNowFragment(BaseFragment show, ImageView tab) {
        changeFragment(show);
        if (mNowShowFragment == null) {
            mFragmentManager.beginTransaction()
                    .add(R.id.f_content, show).commitAllowingStateLoss();
            mNowShowFragment = show;
        }
        tab.setSelected(true);
    }
    
    public void changeFragment(BaseFragment to) {
        if (mNowShowFragment != null) {
            if (mNowShowFragment != to) {
                if (!to.isAdded()) {
                    mFragmentManager.beginTransaction().hide(mNowShowFragment)
                            .add(R.id.f_content, to).commitAllowingStateLoss();
                } else {
                    mFragmentManager.beginTransaction().hide(mNowShowFragment)
                            .show(to).commitAllowingStateLoss();
                }
                mNowShowFragment = to;
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.d("asdf", "onActivityResult");
        if (data != null && data.hasExtra("tab")) {
            String tab = data.getStringExtra("tab");
            if (tab == null) {
                return;
            }
            if (tab.equals("tabB")) {
                toTabB();
            } else if (tab.equals("tabC")) {
                toTabC();
            }
        }
    }
}